import os
from bsccommonlib.csv_persister import CsvPersister
from bsccommonlib.pycommons import abstract
from targetmonitoring.menu_item import MenuItem
from targetmonitoring.target_graphic_data_generator import TargetGraphicDataGenerator
from targetmonitoring.target_status_evulator import TargetStatusEvulator
from targetmonitoring.target_models import TargetObject

    
class AbstractTargetMonitorClient(object):
    def __init__(self, csv_file):
        self.exit = False
        self.csv_file = csv_file
        self.persister = CsvPersister(csv_file)
        self.menu_item_list = [
                         MenuItem(self._exit, 'Exit menu', index = 0)
                        ,MenuItem(self._list_target, 'List targets')
                        ,MenuItem(self._on_submit_entry_request, 'Submit to targets')
                        ,MenuItem(self._display_chart, 'Display chart')
                        ,MenuItem(self._manage_target, 'Manage targets')
                    ]

    @abstract 
    def _on_display_requested(self, selection_list):
        pass

    @abstract
    def _on_target_list_display_request(self, target_list):
        pass

    @abstract
    def _on_manage_target_client_request(self):
        pass

    @abstract
    def _on_submit_to_target_client_request(self, csv_file,target_object):
        pass

    @abstract
    def _on_submit_entry_request(self):
        pass

    @abstract
    def _on_display_chart_entry_request(self):
        pass

    @abstract
    def _on_chart_display_request(self, data_list):
        pass
    
    @abstract
    def _on_exit(self):
        pass

    def _display_chart(self):
        self._on_display_chart_entry_request()
        
    def _exit(self):
        self.exit = True
        self._on_exit()

    def _list_target(self):
        tse = TargetStatusEvulator(self.csv_file)
        self._on_target_list_display_request(tse.get_status_list())

    def _manage_target(self):
        mtm = self._on_manage_target_client_request()
        mtm.start()
        
    def _fire_item_selected(self, selection, **kargs):
        for menu_item in self.menu_item_list:
            if menu_item.index == selection:
                menu_item.bind_method(**kargs)
                break   

    def _fire_submit_entry_completed(self,target_id):
        target_object = TargetObject()
        target_object.id = target_id
        found_item = self.persister.get_item(target_object)
            
        if found_item is not None:  
            client = self._on_submit_to_target_client_request(self._get_target_file_name(target_id), found_item)
            client.start()

    def _fire_display_chart_entry_completed(self,target_id):
        gen = TargetGraphicDataGenerator(self.csv_file, target_id)
        self._on_chart_display_request(gen.get_data())
    
    def _get_target_file_name(self,target_id):
        (path, file_) = os.path.split(self.csv_file)
        return path + '/' + str(target_id) + '.csv'
        
    def start(self):
        while (self.exit is False):
            self._on_display_requested(self.menu_item_list)





